package org.noote.libs.roby.comm;

import org.noote.libs.tcpip.IPClient_ConnectedThread;
import org.noote.libs.tcpip.IPEvent_Client;
import org.noote.libs.graphicbuffer.GraphicBuffer;
import org.noote.libs.roby.configuration.PositionMarker;
import org.noote.libs.roby.scan.ScanResult;

import android.util.Log;

//////////////////////////////////
///
/// 
///
/////////////////////////////////

public class IPCommandEvents implements IPEvent_Client,IPCommandEvents_Interface {
	static final String TAG = "IPCommandHandler";
	protected IPClient_ConnectedThread _thread = null;
	private boolean _isDebug = false;

	@Override
	public void setThread(IPClient_ConnectedThread thread)
	{
		_thread = thread;
	}

	@Override
	public void onConnected() {
	}
	@Override
	public void onDisconnected() {
	}
	@Override
	public void onError(String sError) {
	}
	@Override
	public void onText(String sText) {
	}
	@Override
	public void onCmd(String sMessage)
	{
    	if(_isDebug)
    		Log.d(TAG, "Get cmd : "+sMessage);
		String [] sCmds = sMessage.split("\\:");
		if(sCmds.length==2)
		{
			String sCmd = sCmds[0].trim().toLowerCase();
			String sOps = sCmds[1].trim().toLowerCase();
        	if(sCmd.equals("error"))
        	{
        		if(sOps.length()>0)
    			onError(sOps);
        	}
        	else if(sCmd.equals("message"))
        	{
        		if(sOps.length()>0)
        			onText(sOps);
        	}
        	else if(sCmd.equals("version"))
        	{
        		String robotname = "?", robotversion="?";
				String [] sDatas = sOps.split("\\,");
        		for(String d : sDatas)
        		{
        			String [] sVals = d.split("\\=");
        			if(sVals.length != 2) continue;
        				
        			String name = sVals[0].trim();
        			String val = sVals[1].trim();
        			
        			if(name.equals("name"))
        			{
        				robotname = val;
        			}
        			else if(name.equals("ver"))
        			{
        				robotversion = val;
        			}
        		}
        		
        		onVersion(robotname, robotversion);
        	}
        	else if(sCmd.equals("setting"))
        	{
        		boolean bReport_Transmission = false;
        		String [] sDatas = sOps.split("\\,");
        		for(String d : sDatas)
        		{
        			String [] sVals = d.split("\\=");
        			if(sVals.length != 2) continue;
        			
        			String name = sVals[0].trim();
        			String val = sVals[1].trim();
        			
        			if(name.equals("send_status"))
        			{
        				bReport_Transmission =  val.equals("1")?true:false;
        			}
        		}
        		
        		onSettings(bReport_Transmission);
        	}
        	else if(sCmd.equals("setting_body"))
        	{
        		int iBody_MinSpeed=0, iBody_MidSpeed=0, iBody_MaxSpeed=0, iBody_MinDist=0, iBody_MaxDist=0;
        		String [] sDatas = sOps.split("\\,");
        		for(String d : sDatas)
        		{
        			String [] sVals = d.split("\\=");
        			if(sVals.length != 2) continue;
        			
        			String name = sVals[0].trim();
        			String val = sVals[1].trim();
        			
					if(name.equals("min_speed"))
        				iBody_MinSpeed =  Integer.parseInt(val);
        			else if(name.equals("mid_speed"))
        				iBody_MidSpeed =  Integer.parseInt(val);
        			else if(name.equals("max_speed"))
        				iBody_MaxSpeed =  Integer.parseInt(val);
        			else if(name.equals("min_dist"))
        				iBody_MinDist =  Integer.parseInt(val);
        			else if(name.equals("max_dist"))
        				iBody_MaxDist =  Integer.parseInt(val);
        		}
        		
        		onBodySettings(iBody_MinSpeed, iBody_MidSpeed, iBody_MaxSpeed, iBody_MinDist, iBody_MaxDist);
        	}
        	else if(sCmd.equals("setting_turret"))
        	{
        		int iTurret_MinLow = 0, iTurret_MidLow = 0, iTurret_MaxLow = 0, iTurret_MinHigh = 0, iTurret_MidHigh = 0, iTurret_MaxHigh = 0, iTurret_MinDist = 0, iTurret_MidDist = 0, iTurret_MaxDist = 0;
        		String [] sDatas = sOps.split("\\,");
        		for(String d : sDatas)
        		{
        			String [] sVals = d.split("\\=");
        			if(sVals.length != 2) continue;
        			
        			String name = sVals[0].trim();
        			String val = sVals[1].trim();

        			if(name.equals("min_low"))
        				iTurret_MinLow =  Integer.parseInt(val);
        			else if(name.equals("mid_low"))
        				iTurret_MidLow =  Integer.parseInt(val);
        			else if(name.equals("max_low"))
        				iTurret_MaxLow =  Integer.parseInt(val);
        			else if(name.equals("min_high"))
        				iTurret_MinHigh =  Integer.parseInt(val);
        			else if(name.equals("mid_high"))
        				iTurret_MidHigh =  Integer.parseInt(val);
        			else if(name.equals("max_high"))
        				iTurret_MaxHigh =  Integer.parseInt(val);
        			else if(name.equals("min_dist"))
        				iTurret_MinDist =  Integer.parseInt(val);
        			else if(name.equals("mid_dist"))
        				iTurret_MidDist =  Integer.parseInt(val);
        			else if(name.equals("max_dist"))
        				iTurret_MaxDist =  Integer.parseInt(val);
        		}
        		
        		onTurretSettings(iTurret_MinLow, iTurret_MidLow, iTurret_MaxLow, iTurret_MinHigh, iTurret_MidHigh, iTurret_MaxHigh, iTurret_MinDist, iTurret_MidDist, iTurret_MaxDist);
        	}
        	else if(sCmd.equals("setting_ray"))
        	{
        		int iRay_Count=5, iRay_Delay=5;
        		int iRay_Sensors = 3;
        		float fRay_Precision = 0.30f;
        		String [] sDatas = sOps.split("\\,");
        		for(String d : sDatas)
        		{
        			String [] sVals = d.split("\\=");
        			if(sVals.length != 2) continue;
        			
        			String name = sVals[0].trim();
        			String val = sVals[1].trim();
        			
					if(name.equals("count"))
						iRay_Count =  Integer.parseInt(val);
        			else if(name.equals("delay"))
        				iRay_Delay =  Integer.parseInt(val);
        			else if(name.equals("sensors"))
        				iRay_Sensors =  Integer.parseInt(val);
        			else if(name.equals("precision"))
        				fRay_Precision =  Float.parseFloat(val);
        		}
        		
        		onRaySettings(iRay_Count, iRay_Delay, iRay_Sensors, fRay_Precision);
        	}
        	else if(sCmd.equals("hit_sensors"))
        	{
        		boolean bBody_Switch_BackLeft = false, bBody_Switch_BackCenter = false, bBody_Switch_BackRight = false;
        		String [] sDatas = sOps.split("\\,");
        		for(String d : sDatas)
        		{
        			String [] sVals = d.split("\\=");
        			if(sVals.length != 2) continue;
        			
        			String name = sVals[0].trim();
        			String val = sVals[1].trim();
        			
        			if(name.equals("bl")) bBody_Switch_BackLeft = val.equals("1")?true:false;
        			else if(name.equals("bc")) bBody_Switch_BackCenter = val.equals("1")?true:false;			        			
        			else if(name.equals("br")) bBody_Switch_BackRight = val.equals("1")?true:false;
        		}
        		
        		onHitSensors(bBody_Switch_BackLeft, bBody_Switch_BackCenter, bBody_Switch_BackRight);
        	}
        	else if(sCmd.equals("latest_distance"))
        	{
        		float fBody_LastMoveDistance_Left = 0, _fBody_LastMoveDistance_Right = 0;
        		String [] sDatas = sOps.split("\\,");
        		for(String d : sDatas)
        		{
        			String [] sVals = d.split("\\=");
        			if(sVals.length != 2) continue;
        			
        			String name = sVals[0].trim();
        			String val = sVals[1].trim();
        			
        			if(name.equals("left")) fBody_LastMoveDistance_Left = Float.parseFloat(val);
        			else if(name.equals("right")) _fBody_LastMoveDistance_Right = Float.parseFloat(val);
        		}
        		
        		onLatestDistance(fBody_LastMoveDistance_Left, _fBody_LastMoveDistance_Right);
        	}
        	else if(sCmd.equals("turret"))
        	{
        		int iTurret_Low = 0, iTurret_High = 0, iTurret_Distance = 0;
        		String [] sDatas = sOps.split("\\,");
        		for(String d : sDatas)
        		{
        			String [] sVals = d.split("\\=");
        			if(sVals.length != 2) continue;
        			
        			String name = sVals[0].trim();
        			String val = sVals[1].trim();

        			if(name.equals("low")) iTurret_Low = Integer.parseInt(val);
        			else if(name.equals("high")) iTurret_High = Integer.parseInt(val);
        			else if(name.equals("dist")) iTurret_Distance = Integer.parseInt(val);
        		}
        		
        		onTurretInformation(iTurret_Low, iTurret_High, iTurret_Distance);			        		
        	}
        	else if(sCmd.equals("body"))
        	{
        		float fBody_Bear = 0, fBody_Pitch = 0, fBody_Roll = 0;
        		int iBody_Distance = 0;
        		String [] sDatas = sOps.split("\\,");
        		for(String d : sDatas)
        		{
        			String [] sVals = d.split("\\=");
        			if(sVals.length != 2) continue;
        			
        			String name = sVals[0].trim();
        			String val = sVals[1].trim();

        			if(name.equals("bear")) fBody_Bear = Float.parseFloat(val);
        			else if(name.equals("pitch")) fBody_Pitch = Float.parseFloat(val);
        			else if(name.equals("roll")) fBody_Roll = Float.parseFloat(val);
        			else if(name.equals("dist")) iBody_Distance = Integer.parseInt(val);
        		}
        		
        		onBodyInformation(fBody_Bear, fBody_Pitch, fBody_Roll, iBody_Distance);			        		
        	}
        	else if(sCmd.equals("scan_zone"))
        	{
        		ScanResult scan = new ScanResult();
        		if(scan.parseScanZone(sOps))
        			onScan(scan);
        	}
        	else if(sCmd.equals("scan_line"))
        	{
        		ScanResult scan = new ScanResult();
        		if(scan.parseScanLine(sOps))
        			onScan(scan);
        	}
        	else if(sCmd.equals("position"))
        	{
        		PositionMarker position = PositionMarker.valueFrom(sOps);
        		if(position != null)
        			onPosition(position);
        	}
        	else if(sCmd.equals("camera_image"))
        	{
        		int width = 0, height = 0;
        		String format = "";
        		String [] sDatas = sOps.split("\\,");
        		for(String d : sDatas)
        		{
        			String [] sVals = d.split("\\=");
        			if(sVals.length != 2) continue;
        			
        			String name = sVals[0].trim();
        			String val = sVals[1].trim();

        			if(name.equals("width")) width = Integer.parseInt(val);
        			else if(name.equals("height")) height = Integer.parseInt(val);
        			else if(name.equals("format")) format = val;
        			else if(name.equals("data"))
        			{
        				if(format.equalsIgnoreCase("rgb888"))
        				{
    		        		GraphicBuffer image = new GraphicBuffer();
    		        		
    		        		image._create_outputbuffer(width, height);
    		        		
    		        		int p = 0;
    		        		String [] sPixels = val.split("\\,");
    		        		for(String sPixel : sPixels)
    		        		{
    		        			int iPixel = Integer.parseInt(sPixel);
    		        			image._put_pixel(p++, iPixel);
    		        		}
    		        		
    		        		onCameraImage(image);
        				}
        			}
        		}
        	}
        	else
        		onCommand(sCmd, sOps);
   		}
    }

	@Override
	public void onCommand(String sCommand, String sOperations) {
	}
	@Override
	public void onVersion(String robotname, String robotversion) {
	}
	@Override
	public void onSettings(boolean bReport_Transmission) {
	}
	@Override
	public void onBodySettings(int iBody_MinSpeed, int iBody_MidSpeed,
			int iBody_MaxSpeed, int iBody_MinDist, int iBody_MaxDist) {
	}
	@Override
	public void onTurretSettings(int iTurret_MinLow, int iTurret_MidLow,
			int iTurret_MaxLow, int iTurret_MinHigh, int iTurret_MidHigh,
			int iTurret_MaxHigh, int iTurret_MinDist, int iTurret_MidDist,
			int iTurret_MaxDist) {
	}
	@Override
	public void onRaySettings(int iRay_Count, int iRay_Delay, int iRay_Sensors, float fRay_Precision) {
	}
	@Override
	public void onHitSensors(boolean bBody_Switch_BackLeft,
			boolean bBody_Switch_BackCenter, boolean bBody_Switch_BackRight) {
	}
	@Override
	public void onLatestDistance(float fBody_LastMoveDistance_Left,
			float _fBody_LastMoveDistance_Right) {
	}
    @Override
	public void onTurretInformation(int iTurret_Low, int iTurret_High,
			int iTurret_Distance) {
	}
    @Override
	public void onBodyInformation(float fBody_Bear, float fBody_Pitch,
			float fBody_Roll, int iBody_Distance) {
	}
	@Override
	public void onScan(ScanResult scan) {
	}

	@Override
	public void onHello(String sName, String sVersion) {
	}

	@Override
	public void onPosition(PositionMarker scan) {
	}
	
	@Override
	public void onCameraImage(GraphicBuffer image) {
	}
	
}
